Imports System
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Security.Roles
Imports DotNetNuke.Entities.Portals

Namespace RTP.HTMLNewsletter

#Region "NewsletterBase Class "

    Public Class NewsletterBase
        Inherits PortalModuleBase

#Region "Properties"
        Public CategoryEmail As Integer = 100000000
        Public CategoryYourself As Integer = 110000000
        Public CategoryRoles As Integer = 120000000
        Public CategoryRoleAdd As Integer = 10
        Public CategoryNewsArticles As Integer = 200000000
        Public CategoryNAMultiplier As Integer = 1000

#End Region

#Region "Public Routines"

#Region "Category Routines"
        Public Function RoleToCategoryID(ByVal roleID As Integer) As String
            Return (RoleID + CategoryRoles + CategoryRoleAdd).ToString
        End Function

        Public Function CategoryIDToRole(ByVal categoryID As String) As Integer
            Dim iCategoryid As Integer = CInt(CategoryID)
            If iCategoryid > CategoryRoles And iCategoryid < CategoryNewsArticles Then
                Return iCategoryid - CategoryRoles - CategoryRoleAdd
            End If
            Return -1
        End Function

        Public Function NAModToCategory(ByVal moduleLevelID As String) As String
            Dim iModuleLevelID As Integer = CInt(ModuleLevelID)
            Return ((iModuleLevelID * CategoryNAMultiplier) + CategoryNewsArticles).ToString
        End Function
#End Region

#Region "Newsletter Updating Routines"
        Public Function SendNewsletterYourself(ByVal nlUserid As Integer, ByVal nlEmail As String, ByVal nLitemID As Integer, ByVal nlPublishDate As DateTime) As Boolean
            Try
                Dim nlNewsletterCategoryID As Integer = AddNewsletterCategory(nLitemID, CategoryYourself)

                Dim objCtlNewsletterUser As New NewsletterUserController
                objCtlNewsletterUser.AddNewsletterUser(nLitemID, nlUserid, False, False, nlEmail, nlNewsletterCategoryID, nlPublishDate, PortalAlias.PortalAliasID)

            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try

            Return True
        End Function

        Public Function SendNewsletterRole(ByVal modSettings As Hashtable, ByVal nlPortalSettings As PortalSettings, ByVal roleID As Integer, ByVal nlItemID As Integer, ByVal publish As Boolean, ByVal nlPublishDate As DateTime) As Boolean
            Dim txtIgnoreEmail As String = ""
            If CType(modSettings("ignoreemail"), String) <> "" Then
                txtIgnoreEmail = CType(modSettings("ignoreemail"), String)
            End If

            Dim categoryID As String = RoleToCategoryID(roleID)

            Dim objRoleCtl As New RoleController
            Dim objRole As RoleInfo
            objRole = objRoleCtl.GetRole(roleID, nlPortalSettings.PortalId)
            If Not objRole Is Nothing Then
                Dim arrRolemembers As ArrayList = objRoleCtl.GetUsersByRoleName(nlPortalSettings.PortalId, objRole.RoleName)
                Dim nlNewsletterCategoryID As Integer = AddNewsletterCategory(nlItemID, CInt(categoryID))
                If publish Then
                    Try
                        For Each objUserRoleInfo As UserInfo In arrRolemembers
                            Dim objCtlNewsletterUser As New NewsletterUserController
                            Dim blIgnore As Boolean = False
                            If objUserRoleInfo.Email = txtIgnoreEmail Then
                                blIgnore = True
                            End If
                            objCtlNewsletterUser.AddNewsletterUser(nlItemID, objUserRoleInfo.UserID, blIgnore, False, objUserRoleInfo.Email, nlNewsletterCategoryID, nlPublishDate, PortalAlias.PortalAliasID)
                        Next

                    Catch exc As Exception
                        ProcessModuleLoadException(Me, exc)
                    End Try
                End If
            End If

            Return True
        End Function

        Public Function AddNewsletter(ByVal nlSummary As String, ByVal nlBody As String, ByRef nlSubject As String, ByVal nlUserId As Integer, ByVal nlModuleID As Integer, _
                                      ByVal nlArticleID As Integer, ByVal nlArticleTabModuleID As Integer, ByVal nlPublished As Boolean, ByVal nlPublishDate As DateTime, _
                                      ByVal modSettings As Hashtable, ByVal inPortalID As Integer) As Integer
            Dim nlEmail As String = GetUserEmail(nlUserId, modSettings)
            Dim objCtlNewsletter As New NewsletterController
            Return objCtlNewsletter.AddNewsletter(nlSummary, nlBody, nlSubject, "", -1, nlUserId, nlEmail, nlModuleID, nlArticleID, nlArticleTabModuleID, nlPublished, nlPublishDate, inPortalID)
        End Function

        Public Function UpdateNewsletter(ByVal newsletterID As Integer, ByVal nlSummary As String, ByVal nlBody As String, ByRef nlSubject As String, ByVal nlUserId As Integer, ByVal nlModuleID As Integer, _
                                         ByVal nlArticleID As Integer, ByVal nlArticleTabModuleID As Integer, ByVal published As Boolean, ByVal nlPublishDate As DateTime, ByVal modSettings As Hashtable) As Boolean
            Dim nlEmail As String = GetUserEmail(nlUserId, modSettings)
            Dim objCtlNewsletter As New NewsletterController
            objCtlNewsletter.UpdateNewsletter(newsletterID, nlSummary, nlBody, nlSubject, "", -1, nlUserId, nlEmail, nlModuleID, nlArticleID, nlArticleTabModuleID, published, nlPublishDate)
            Return True
        End Function

        Public Function DeleteNewsletter(ByVal newsletterID As Integer) As Boolean
            Dim objCtlNewsletter As New NewsletterController
            objCtlNewsletter.DeleteNewsletter(NewsletterID)
            Return True
        End Function

        Public Function AddNewsletterCategory(ByVal newsletterID As Integer, ByVal categoryID As Integer) As Integer
            Dim objNewsletterCategoryCtl As New NewsletterCategoryController
            Dim nlNewsletterCategoryID As Integer
            Dim objNewsletterCategoryInfo As NewsletterCategoryInfo = objNewsletterCategoryCtl.GetNewsletterCategoryByNewsletterIDCategoryID(NewsletterID, CategoryID)
            If Not objNewsletterCategoryInfo Is Nothing Then
                nlNewsletterCategoryID = objNewsletterCategoryInfo.NewsletterCategoryID
            Else
                nlNewsletterCategoryID = objNewsletterCategoryCtl.AddNewsletterCategory(NewsletterID, CategoryID)
            End If
            Return nlNewsletterCategoryID
        End Function

#End Region

#Region "Ventrian News Articles Updating Routines"
        Public Function UpdateVentrianNAImages(ByVal articleID As Integer) As Boolean
            If ArticleID > 0 Then
                Dim objVentrianNACtl As New VentrianNAController
                objVentrianNACtl.UpdateVentrianNAImages(ArticleID)
            End If
            Return True
        End Function
#End Region

#Region "General Routines"
        Public Function GetDomainURL(ByVal ps As Entities.Portals.PortalSettings) As String
            Dim domainurl As String = HttpContext.Current.Request.ServerVariables("HTTP_HOST")
            If domainurl Is Nothing Then
                domainurl = ps.PortalAlias.HTTPAlias()
            End If
            If domainurl.IndexOf("/", StringComparison.Ordinal) > 0 Then
                domainurl = domainurl.Substring(0, domainurl.IndexOf("/", StringComparison.Ordinal))
            End If
            Return domainurl
        End Function

        Public Function GetUserEmail(ByVal iUserID As Integer, ByVal modSettings As Hashtable) As String
            Dim users As New UserController
            Dim objUserInfo As UserInfo = users.GetUser(PortalId, iUserID)
            Dim userEmail As String = objUserInfo.Email
            If CType(modSettings("profilefield"), String) <> "" Then
                Dim txtProfileField As String = CType(modSettings("profilefield"), String)
                Dim txtUseridProfile As String
                txtUseridProfile = objUserInfo.Profile.GetPropertyValue(txtProfileField)
                If txtUseridProfile <> "" Then
                    userEmail = txtUseridProfile
                End If
            End If

            Return userEmail

        End Function

        Public Function GetPortalRoles(ByVal inPortalID As Integer) As ArrayList
            Dim objRoleCtl As New RoleController
            Dim arrRoles As ArrayList = objRoleCtl.GetPortalRoles(inPortalID)
            Return arrRoles
        End Function

        Public Function MetaPostAndVentrianInstalled() As Boolean
            Dim objmetaPostModuleInfo As DesktopModuleInfo = DesktopModuleController.GetDesktopModuleByModuleName("itcMetaPost", PortalId)
            If objmetaPostModuleInfo Is Nothing Then
                Return False
            End If

            Dim objVentrianModuleInfo As DesktopModuleInfo = DesktopModuleController.GetDesktopModuleByModuleName("DnnForge - NewsArticles", PortalId)
            If objVentrianModuleInfo Is Nothing Then
                Return False
            End If

            Return True
        End Function

#End Region

#End Region

#Region "Private Routines"
#End Region


    End Class
#End Region
End Namespace